String clazzTpl({package}) => """
import 'dart:async';
import 'dart:typed_data';
import 'redux_state.dart';
import 'package:redux_persist/redux_persist.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:$package/utils/encrypt_util.dart';
import 'package:$package/utils/print_util.dart';

class SYSharedPreferencesEngine implements StorageEngine {
  SYSharedPreferencesEngine([this.key = "app"]);

  final String key;

  @override
  Future<Uint8List?> load() async {
    final sharedPreferences = await _getSharedPreferences();
    return stringToUint8List(
        EncryptUtil.decryptAES(sharedPreferences.getString(key)));
  }

  @override
  Future<void> save(Uint8List? data) async {
    final sharedPreferences = await _getSharedPreferences();
    sharedPreferences.setString(
        key, EncryptUtil.encryptAES(uint8ListToString(data)!) ?? '');
  }

  Future<bool> clean() async {
    final sharedPreferences = await _getSharedPreferences();
    return sharedPreferences.remove(key);
  }

  Future<SharedPreferences> _getSharedPreferences() async =>
      await SharedPreferences.getInstance();
}

class PersistorEngine {
  PersistorEngine(this.key) {
    _engine = SYSharedPreferencesEngine(key);
    _persistor = Persistor<ReduxState>(
      storage: _engine,
      serializer: JsonSerializer<ReduxState>(ReduxState.fromJson),
    );
  }

  final String key;
  late Persistor<ReduxState> _persistor;
  late SYSharedPreferencesEngine _engine;

  Persistor<ReduxState> get persistor => _persistor;

  Future<ReduxState> get initialState async => await _loadState();

  Future<ReduxState> _loadState() async {
    try {
      final data = await _persistor.load();
      return data as ReduxState;
    } catch (e) {
      logPrint(e);
      await _engine.clean();
      final data = await _persistor.load();
      return data as ReduxState;
    }
  }
}
""";
